package com.example.stujava.module.xiangxue.reflect.generic_type;

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.WildcardType;
import java.util.List;

public class WildcardTypeStu {

    private List<? extends Number> a;
    private List<? super String> b;

    public static void main(String[] args) throws Exception {
        Field fieldA = WildcardTypeStu.class.getDeclaredField("a");
        Field fieldB = WildcardTypeStu.class.getDeclaredField("b");
        //拿到泛型类型
        ParameterizedType pTypeA = (ParameterizedType) fieldA.getGenericType();
        ParameterizedType pTypeB = (ParameterizedType) fieldB.getGenericType();
        //从泛型类型中拿到通配符类型
        WildcardType actualTypeArgument = (WildcardType) pTypeA.getActualTypeArguments()[0];
        WildcardType wTypeB = (WildcardType) pTypeB.getActualTypeArguments()[0];
        //通配符类型
        System.out.println(actualTypeArgument.getUpperBounds()[0]);//class java.lang.Number
        System.out.println(wTypeB.getUpperBounds()[0]);//class java.lang.Object
        //通配符格式
        System.out.println(wTypeB);//? super java.lang.String
    }
}
